<template>
    <div class="list">
        <div
            class="item"
            v-for="(item, key) of letters"
            :key="key"
            :ref="item"
            @click="handleletterClick"
            @touchstart="touchstart"
            @touchmove="handlerTouchMove"
            @touchend="handlerTouchEnd"
        >
            {{ item }}
        </div>
    </div>
</template>

<script>
export default {
    name: "alphabet",
    data() {
        return {
            touchStatus: false,
            startY: 0,
            timer: true,
        };
    },
    updated() {
        this.startY = this.$refs["A"][0].offsetTop;
    },
    props: {
        cities: Object,
    },
    computed: {
        letters() {
            const letters = [];
            for (let i in this.cities) {
                letters.push(i);
            }
            return letters;
        },
    },
    methods: {
        handleletterClick(e) {
            console.log("handleletterClick");
            // console.log(e.target.innerText)
            this.$emit("change", e.target.innerText);
        },
        touchstart() {
            this.touchStatus = true;
            console.log("touchstart");
        },
        handlerTouchMove(e) {
            console.log("handlerTouchMove");
            if (this.touchStatus) {
                if (this.timer) {
                    clearTimeout(this.timer);
                }
                this.timer = setTimeout(() => {
                    // const startY = this.$refs["A"][0].offsetTop;
                    const touchY = e.touches[0].clientY - 86;
                    const index = Math.floor((touchY - this.startY) / 20);
                    if (index >= 0 && index < this.letters.length) {
                        this.$emit("change", this.letters[index]);
                    }
                }, 16);
            }
        },
        handlerTouchEnd() {
            this.touchStatus = false;
        },
    },
};
</script>

<style lang="scss" scoped>
@import "@/assets/css/main.scss";
.list {
    position: absolute;
    top: 86px;
    right: 0;
    bottom: 0;
    width: 20px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    .item {
        text-align: center;
        color: $maincolor;
        line-height: 20px;
    }
}
</style>